package com.k12.user.service.impl;

import com.k12.user.model.entity.SysUser;
import com.k12.user.mapper.SysUserDao;
import com.k12.user.service.SysUserService;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
 * 用户信息表(SysUser)表服务实现类
 *
 * @author makejava
 * @since 2025-07-28 20:53:09
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserDao sysUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public SysUser queryById(Long userId) {
        return this.sysUserDao.queryById(userId);
    }

    /**
     * 分页查询
     *
     * @param sysUser 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public List<SysUser> queryByPage(SysUser sysUser) {
        return this.sysUserDao.queryAllByLimit(sysUser);
    }

    /**
     * 新增数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public SysUser insert(SysUser sysUser) {
        this.sysUserDao.insert(sysUser);
        return sysUser;
    }

    /**
     * 修改数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public SysUser update(SysUser sysUser) {
        this.sysUserDao.update(sysUser);
        return this.queryById(sysUser.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long userId) {
        return this.sysUserDao.deleteById(userId) > 0;
    }

    @Override
    public SysUser getLogin(String userName, String password) {
        return this.sysUserDao.getLogin(userName,password);
    }

    @Override
    public String checkRegister(SysUser sysUser) {
        SysUser sysUser1 = new SysUser();
        sysUser1.setEmail(sysUser.getEmail());
        List<SysUser> sysUsers1 = this.sysUserDao.queryAllByLimit(sysUser1);
        if (!sysUsers1.isEmpty()){
            return "失败，邮箱账号已存在";
        }
        SysUser sysUser2 = new SysUser();
        sysUser2.setPhonenumber(sysUser.getPhonenumber());
        List<SysUser> sysUsers2 = this.sysUserDao.queryAllByLimit(sysUser2);
        if (!sysUsers2.isEmpty()){
            return "失败，手机号码已存在";
        }
        SysUser sysUser3 = new SysUser();
        sysUser3.setUserId(sysUser.getUserId());
        List<SysUser> sysUsers3 = this.sysUserDao.queryAllByLimit(sysUser3);
        if (!sysUsers3.isEmpty()){
            return "失败，登录账号已存在";
        }
        return "";
    }

    @Override
    public Long addUser(SysUser sysUser) {
        this.sysUserDao.insert(sysUser);
        List<SysUser> sysUsers = this.sysUserDao.queryAllByLimit(sysUser);
        return sysUsers.get(0).getUserId();
    }

    @Override
    public List<String> listPermission(Long userId) {
        SysUser sysUser = sysUserDao.queryById(userId);
        //00、超级管理员具有满权限
        if("00".equals(sysUser.getUserType())){
            return new ArrayList<>(){{add("*");}};
        }
        return sysUserDao.getPermissionList(userId);
    }

    @Override
    public SysUser selectUserById(String tenantId, Long userId) {
        SysUser sysUser = new SysUser();
        sysUser.setTenantId(Long.valueOf(tenantId));
        sysUser.setUserId(userId);
        return sysUserDao.queryAllByLimit(sysUser).get(0);
    }

    @Override
    public void deleteSysUser(Long userId, String tenantId) {
        sysUserDao.deleteByUserIdAndTenantId(userId,tenantId);
    }

    @Override
    public int checkDeptExistUser(Long userId, String tenantId) {
        return sysUserDao.checkDeptExistUser(userId,tenantId);
    }
}
